package com.hobbo.widget.chronograph;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author zsc
 * @create 2019/3/26 0026
 * @Describe
 */
public class ChronographProgress extends View implements ICycle {


    private static final String TAG = ChronographProgress.class.getSimpleName();

    /**
     * 状态
     */
    private int mState;

    /**
     * 外圆半径
     */
    private int mOutsideCircleRadius;
    /**
     * 内圆半径
     */
    private int mInsideCircleRadius;

    /**
     * 画园的画笔
     */
    private Paint mCirclePaint = new Paint();


    private SimpleDateFormat mFormat = new SimpleDateFormat(" mm:ss.SS", Locale.getDefault());

    private Timer mTimer;
    private int mOutsideProgressRadius;
    private RectF mArcOval = new RectF();
    private float mStrokeWidth;
    private Point mCenterPoint;
    private int mValue;
    private Paint mPaint = new Paint();
    private int mWidth;
    private int mHeight;
    private String mText;
    private RectF mBgRect;

    private  Paint mTextPaint;

    //总刻度
    private int mTotalScale = 360 * 60 * 5;

    public ChronographProgress(Context context) {
        super(context);
    }


    public ChronographProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawProgress(canvas);
        drawTimeText(canvas);
    }

    /**
     * 画时间
     * @param canvas
     */
    private void drawTimeText(Canvas canvas) {
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        float baseline = mBgRect.top + (mBgRect.height() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        if (mText != null && mText.length() > 0) {
            canvas.drawText(mText, mBgRect.centerX(), baseline, mTextPaint);
        } else {
            canvas.drawText(mFormat.format(0), mBgRect.centerX(), baseline, mTextPaint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        mHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        configShape();
        setMeasuredDimension(mWidth, mHeight);
        setBackground();
    }

    /**
     * 设置背景图片
     */
    private void setBackground() {
        //创建空白背景
        Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_4444);
        //使用画布跟画笔创建bitmap
        drawDial(new Canvas(bitmap));
        //设置给View
        setBackground(new BitmapDrawable(bitmap));
    }

    /**
     * 自适应
     */
    private void configShape() {
        mOutsideCircleRadius = (int) (((Math.min(mWidth, mHeight)) * 0.98f) / 2);
        mInsideCircleRadius = (int) (mOutsideCircleRadius * 0.95f);
        mOutsideProgressRadius = (int) (mOutsideCircleRadius * 0.70f);
        mStrokeWidth = mOutsideCircleRadius * 0.15f;
        mCenterPoint = new Point(mWidth / 2, mHeight / 2);
        mBgRect = new RectF(getPaddingLeft(), getPaddingTop(), mWidth + getPaddingLeft(), mHeight + getPaddingTop());
        mTextPaint  = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.parseColor("#AA000000"));
        mTextPaint.setTextSize(mOutsideCircleRadius / 5);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    /**
     * 画progress
     * @param canvas
     */
    private void drawProgress(Canvas canvas) {
        Paint backgroundPaint = new Paint();
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setColor(Color.parseColor("#F0F0F0"));
        backgroundPaint.setAlpha(200);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(mStrokeWidth);
        canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mOutsideProgressRadius, backgroundPaint);
        mArcOval.left = mCenterPoint.x - mOutsideProgressRadius;
        mArcOval.right = mCenterPoint.x + mOutsideProgressRadius;
        mArcOval.top = mCenterPoint.y - mOutsideProgressRadius;
        mArcOval.bottom = mCenterPoint.y + mOutsideProgressRadius;
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setColor(Color.parseColor("#110000FF"));
        int startAngle = (270 + mValue) % 360;
        canvas.drawArc(mArcOval, startAngle, 135, false, mPaint);
    }

    @Override
    public void start() {
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mValue++;
                mText = mFormat.format(mValue);
                invalidate();
            }
        }, 0, 1);
        mState=1;
    }

    @Override
    public void pause() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        mState=2;
    }


    @Override
    public void reset() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        mValue = 0;
        mText = null;
        mState=0;
        invalidate();
    }


    /**
     * 背景
     *
     * @param canvas
     */
    private void drawDial(Canvas canvas) {
        mCirclePaint.setStrokeWidth(0.3f);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setColor(Color.GRAY);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mOutsideCircleRadius, mCirclePaint);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mInsideCircleRadius, mCirclePaint);
        Paint scaleBigPaint = new Paint();
        scaleBigPaint.setStrokeWidth(0.15f);
        scaleBigPaint.setAntiAlias(true);
        scaleBigPaint.setColor(Color.BLACK);
        float scaleBigLength = mOutsideCircleRadius * 0.05f;
        Paint scaleSmallPaint = new Paint();
        scaleSmallPaint.setStrokeWidth(0.05f);
        scaleSmallPaint.setAntiAlias(true);
        scaleSmallPaint.setColor(Color.parseColor("#AA888888"));
        float scaleSmallLength = mOutsideCircleRadius * 0.03f;
        //总刻度

        for (int scale = 0; scale < mTotalScale; scale++) {
            float smallAngle = 360 / 60 / 4;
            double angle = (double) -smallAngle * scale;
            double cos = Math.cos(Math.toRadians(angle + 180));
            double sin = Math.sin(Math.toRadians(angle + 180));
            float x0 = (float) (getMeasuredWidth() / 2 + mInsideCircleRadius * sin);
            float y0 = (float) (getMeasuredWidth() / 2 + mInsideCircleRadius * cos);
            float x1 = 0;
            float y1 = 0;
            if (scale % 5 == 0 || scale == 0) {
                x1 = (float) (getMeasuredWidth() / 2 + (mInsideCircleRadius - scaleBigLength) * sin);
                y1 = (float) (getMeasuredHeight() / 2 + (mInsideCircleRadius - scaleBigLength) * cos);
                canvas.drawLine(x0, y0, x1, y1, scaleBigPaint);
            } else {
                x1 = (float) (getMeasuredWidth() / 2 + (mInsideCircleRadius - scaleSmallLength) * sin);
                y1 = (float) (getMeasuredHeight() / 2 + (mInsideCircleRadius - scaleSmallLength) * cos);
                canvas.drawLine(x0, y0, x1, y1, scaleSmallPaint);
            }
        }
    }

    public int getState() {
        return mState;
    }
}



